﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace scavenger.winforms
{
    public partial class AddDownloadBox : Form
    {
        public bool ShowScheduled = false;

        public AddDownloadBox()
        {
            InitializeComponent();
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmdStart_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            //TODO: Validation
            if (optSchedule.Checked)
            {
                if (scheduleDate.Value < DateTime.Now)
                {
                    MessageBox.Show("Scheduled time is less than current time.",Application.ProductName,MessageBoxButtons.OK,MessageBoxIcon.Information);
                    return;
                }
            }

            Uri url = new Uri(txtUrl.Text);
            HttpDownload download = new HttpDownload(url, txtLocalPath.Text + lblFileName.Text);
            
            MainForm.HttpDownloads.Add(download);

            download.CryptoAlgo = CryptoAlgoEnum.None;
            if (chkVerify.Checked)
            {
                download.CryptoAlgo= (CryptoAlgoEnum)Enum.Parse(typeof(CryptoAlgoEnum), cmbVerify.Text);
            }
            else {
            }
            download.CryptoKey = txtVerify.Text;
            //download.OnProgress += MainForm.OnProgress;
            if (optSchedule.Checked)
                download.Schedule(scheduleDate.Value);
            else
                download.Start();

            if (SettingsManager.settings["proxy_server"].value.Length > 0)
            {
                download.UseProxy = true;
                download.ProxyServer = SettingsManager.settings["proxy_server"].value;
                download.ProxyServer = SettingsManager.settings["proxy_port"].value;
                download.ProxyServer = SettingsManager.settings["proxy_username"].value;
                download.ProxyServer = SettingsManager.settings["proxy_password"].value;
            }
    
            this.Close();

            Cursor.Current = Cursors.Default;
        }

        private void AddDownloadBox_Load(object sender, EventArgs e)
        {
            //TODO:
            //1. Create a customized select-folder-dialog
            //2. enhanced soft-search for url box.

            txtLocalPath.Text = AppDomain.CurrentDomain.BaseDirectory + "downloads" + System.IO.Path.DirectorySeparatorChar;
            lblFileName.Text = "";
            txtUrl_TextChanged(null, null);
            if (ShowScheduled)
                optSchedule.Checked = true;

            cmbVerify.SelectedIndex = 0;
        }

        private void txtUrl_TextChanged(object sender, EventArgs e)
        {
            if (!Uri.IsWellFormedUriString(txtUrl.Text, UriKind.Absolute))
                return;
            Uri url = new Uri(txtUrl.Text);
            string filename = System.IO.Path.GetFileName(url.LocalPath);
            lblFileName.Text = filename;
        }

        private void optSchedule_CheckedChanged(object sender, EventArgs e)
        {
            scheduleDate.Enabled = (optSchedule.Checked);
        }

        private void optNow_CheckedChanged(object sender, EventArgs e)
        {
            scheduleDate.Enabled = (optSchedule.Checked);
        }

        private void chkVerify_CheckedChanged(object sender, EventArgs e)
        {
            groupBox1.Enabled = chkVerify.Checked;
        }

        private void lblFileName_Click(object sender, EventArgs e)
        {

        }

        private void cmdBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowDialog();
            if (fbd.SelectedPath.Length > 0)
                txtLocalPath.Text = fbd.SelectedPath;
        }
    }
}
